﻿#ifndef WAVFILETOOLS_H
#define WAVFILETOOLS_H
#include <QtCore>
#include "string"


// wav文件头信息结构
struct WAVFILEHEADER
{
    // RIFF 头;
    char RiffName[4];
    unsigned long nRiffLength;

    // 数据类型标识符;
    char WavName[4];

    // 格式块中的块头;
    char FmtName[4];
    unsigned long nFmtLength;

    // 格式块中的块数据;
    unsigned short nAudioFormat;
    unsigned short nChannleNumber;
    unsigned long nSampleRate;
    unsigned long nBytesPerSecond;
    unsigned short nBytesPerSample;
    unsigned short nBitsPerSample;

    // 附加信息(可选),根据 nFmtLength 来判断;
    // 扩展域大小;
    unsigned short nAppendMessage;
    // 扩展域信息;
    char* AppendMessageData;

    //Fact块,可选字段，一般当wav文件由某些软件转化而成，则包含该Chunk;
    char FactName[4];
    unsigned long nFactLength;
    char FactData[4];

    // 数据块中的块头;
    char    DATANAME[4];
    unsigned long   nDataLength;

    // 以下是附加的一些计算信息;
    uint64_t fileDataSize;				// 文件音频数据大小;
    uint16_t fileHeaderSize;				// 文件头大小;
    uint64_t fileTotalSize;				// 文件总大小;


    // 理论上应该将所有数据初始化，这里只初始化可选的数据;
    WAVFILEHEADER()
    {
        memset(this,0,sizeof (WAVFILEHEADER));
        nAppendMessage = 0;
        AppendMessageData = NULL;
        memset(FactName, 0,4);
        nFactLength = 0;
        memset(FactData, 0,4);
    }

};

class WavFileTools
{
public:
    WavFileTools();
    static bool anlysisWavFileHeader(QString fileName,WAVFILEHEADER &WavFileHeader);
    static uint64_t timeToIndex(uint32_t sampleRate,uint16_t channleNumber,uint16_t bitsPerSample,double time);
    static double indexToTime(uint32_t sampleRate,uint16_t channleNumber,uint16_t bitsPerSample,uint64_t index);

};

#endif // WAVFILETOOLS_H
